package com.tablefood.dao.Impl;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.tablefood.dao.OrderDao;
import com.tablefood.domain.ShopOrder;

public class OrderDaoImpl implements OrderDao {

	@Resource
	private SessionFactory sessionFactory;   
	
	/*生成一个新订单*/
	@Override
	public ShopOrder insertOrder(ShopOrder order) {
		System.out.println("保存订单前userid:"+order.getShopUser().getUserid());
		System.out.println("保存订单前orderid:"+order.getOrderid());
		sessionFactory.getCurrentSession().save(order);  
		System.out.println("成功保存后orderid:"+order.getOrderid());
		System.out.println("订单用户："+order.getShopUser().getUserid());
		return order;
	}
	
	/*更新部分字段*/
	@Override
	public ShopOrder updateOrder(ShopOrder order) {
		System.out.println("更新订单");
		sessionFactory.getCurrentSession().update(order);
		System.out.println("更新结束");
		return order;
	}
	
	/*
	 * 根据用户userid和支付状态查询订单
	 * */
	@Override
	public List searchOrder(String userid,String status) {
		
		String sql = "from ShopOrder as order where order.shopUser.userid=? and order.status=?";
		System.out.println("查找用户基本语句："+sql);
		 
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, userid);
		query.setString(1,status);
		 
		System.out.println("---查询结果----");
		System.out.println("是否有结果query.list().size()："+query.list().size());
		if(query.list().size()==0){
			
			System.out.println("没有数据");
			return null ;
			
		}else{
			System.out.println("返回result");
			List result = query.list(); 
			return result;
		}
	}

	@Override
	public List<ShopOrder> searchOrder(String userid) {
		String sql = "from ShopOrder as order where order.shopUser.userid=? ";
		System.out.println("查找用户基本语句："+sql);
		 
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, userid);
		
		 
		System.out.println("---查询结果----");
		System.out.println("是否有结果query.list().size()："+query.list().size());
		if(query.list().size()==0){
			
			System.out.println("没有数据");
			return null ;
			
		}else{
			System.out.println("返回result");
			List result = query.list(); 
			return result;
		}
	}
	
	
	@Override
	public List findOrder(ShopOrder order) {
		
		System.out.println("支付状态："+order.getStatus());
		
		String sql = "from ShopOrder as order where order.userid=? and order.status=? and order.orderid=?";
		System.out.println("查找未支付订单基本语句："+sql);
		
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, Long.toString(order.getOrderid()));
		query.setString(1, "0");
		
		
		System.out.println("---查询结果----");
		System.out.println("是否有结果query.list().size()："+query.list().size()); 
		System.out.println("返回result");
		List result = query.list(); 
		
		return result;
	}

	/*
	 * 根据操作时间找到最近的订单
	 * */
	@Override
	public List searchOrderByTime(String orderid, String updatetime) {
	
		String sql = "from ShopOrder as order where order.orderid=?and order.updatetime=?";
		
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, orderid);
		query.setString(1,updatetime );
		System.out.println("查找最近操作的订单基本语句："+sql);
		System.out.println("是否有结果query.list().size()："+query.list().size()); 
		if(query.list().size()!=0){
			List<ShopOrder> result = query.list();
			return result;
		}else{ 
			return null;
		}
		
	}

	@Override
	public ShopOrder searchOrderByOrderId(Long orderid) {
		
		String sql = "from ShopOrder as order where order.orderid=?";
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setLong(0, orderid);
		if(query.list().size()>0){
			List<ShopOrder> list = query.list();
			ShopOrder order = list.get(0);
			return order;
		}
		else
			return null;
	}

	@Override
	public boolean delByorderid(ShopOrder order) {
		
		try{
		String sql = "Delete from ShopOrder as order where order.orderid=";
		sessionFactory.getCurrentSession().createQuery(sql+ order.getOrderid()).executeUpdate(); 
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public List<ShopOrder> queryForPage(String hql, int offset, int length) {
		 
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setFirstResult(offset);
		query.setMaxResults(length);
		List<ShopOrder> list = query.list();
		return list;
	 
	}

	@Override
	public int getAllRowCount(String hql) {
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return query.list().size();
	}

	
	



	

}
